package com.teamcenter.rac.massupdate.databinding;

import org.apache.log4j.Logger;
import org.eclipse.core.databinding.observable.value.WritableValue;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.views.properties.IPropertySource;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventHandler;

import com.teamcenter.rac.aifrcp.event.ClientEventDispatcher;
import com.teamcenter.rac.kernel.TCComponent;
import com.teamcenter.rac.kernel.TCSession;
import com.teamcenter.rac.massupdate.Activator;
import com.teamcenter.rac.massupdate.model.UpdateJobModel;
import com.teamcenter.rac.massupdate.ui.Messages;
import com.teamcenter.rac.massupdate.utils.SourceHelper;
@SuppressWarnings({"rawtypes", "deprecation"})
public abstract class AbstractModelObservable
  extends WritableValue
  implements EventHandler
{
  private ServiceRegistration m_eventServiceRegistration;
  private String m_imageDescriptorPropertyName;
  private String m_sourcePropertyName;
  private TCSession m_tcSession;
  private UpdateJobModel m_updateJobModel;
  
  public AbstractModelObservable() {}
  
  protected AbstractModelObservable(Object paramObject1, Object paramObject2)
  {
    super(paramObject1, paramObject2);
  }
  
  protected AbstractModelObservable(UpdateJobModel paramUpdateJobModel)
  {
    setUpdateJobModel(paramUpdateJobModel);
  }
  
  public UpdateJobModel getUpdateJobModel()
  {
    return this.m_updateJobModel;
  }
  
  public void handleEvent(Event paramEvent)
  {
    String str = paramEvent.getTopic();
    Object localObject;
    if (str.equals("com/teamcenter/rac/event/Thumbnail2D/Response"))
    {
      localObject = paramEvent.getProperty("Input");
      IPropertySource localIPropertySource = (IPropertySource)getUpdateJobModel().getPropertyValue(this.m_sourcePropertyName);
      if (localIPropertySource == null) {
        return;
      }
      TCComponent localTCComponent = SourceHelper.getAssociatedComponent(localIPropertySource, getTCSession());
      if ((localObject == localTCComponent) && (this.m_eventServiceRegistration != null) && (!isDisposed()))
      {
        ClientEventDispatcher.removeEventHandler(this.m_eventServiceRegistration);
        this.m_eventServiceRegistration = null;
        ImageDescriptor localImageDescriptor = (ImageDescriptor)paramEvent.getProperty("OutputImageDescription");
        getUpdateJobModel().setPropertyValue(this.m_imageDescriptorPropertyName, localImageDescriptor);
      }
    }
    else
    {
      localObject = new StringBuffer(Messages.Exception_UnexpectedEvent);
      ((StringBuffer)localObject).append(" ");
      ((StringBuffer)localObject).append(paramEvent);
      throw new IllegalArgumentException(((StringBuffer)localObject).toString());
    }
  }
  
  public void setUpdateJobModel(UpdateJobModel paramUpdateJobModel)
  {
    this.m_updateJobModel = paramUpdateJobModel;
  }
  
  protected void fireThumbnailImageRequest(TCComponent paramTCComponent, String paramString1, String paramString2)
  {
    if (this.m_eventServiceRegistration == null) {
      this.m_eventServiceRegistration = ClientEventDispatcher.addEventHandler(this, "com/teamcenter/rac/event/Thumbnail2D/Response");
    }
    this.m_sourcePropertyName = paramString1;
    this.m_imageDescriptorPropertyName = paramString2;
    ClientEventDispatcher.fireEventLater(this, "com/teamcenter/rac/event/Thumbnail2D/Request", new Object[] { "Input", paramTCComponent, "RETURN_TYPE_ICON", Boolean.TRUE });
  }
  
  protected TCSession getTCSession()
  {
    try
    {
      if (this.m_tcSession == null) {
        this.m_tcSession = ((TCSession)Activator.getDefault().getSessionService().getSession(TCSession.class.getName()));
      }
    }
    catch (Exception localException)
    {
      Logger.getLogger(AbstractModelObservable.class).error(localException.getLocalizedMessage(), localException);
    }
    return this.m_tcSession;
  }
}
